using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Data.SqlClient;
using Acms.SqlDal;

/// <summary>
/// Summary description for BaseItem
/// </summary>
public abstract class BaseItem
{
    protected BaseItem()
    {
    }

    protected BaseItem(object concurencyId)
    {
        this.concurencyId = concurencyId;
    }

    private void PerformValidation()
    {
        List<ValidationExceptionItem> items = new List<ValidationExceptionItem>();
        Validate(items);
        if (items.Count > 0)
        {
            ValidationException exception = new ValidationException();
            exception.Items = items.ToArray();
            throw exception;
        }
    }

    protected abstract void Validate(List<ValidationExceptionItem> errors);

    private bool isNew = true;
    public bool IsNew
    {
        get { return isNew; }
    }

    private bool isDeleted;
    public bool IsDeleted
    {
        get { return isDeleted; }
    }

    public void MarkAsDeleted()
    {
        isDeleted = true;
    }

    private object concurencyId;
    public object ConcurencyId
    {
        get { return concurencyId; }
    }
    protected void SetConcurecyId(object val)
    {
        concurencyId = val;
    }

    public bool CheckConcurent(object concurencyId)
    {
        bool isConcurrent = false;
        if (this.ConcurencyId == null && concurencyId == null)
        {
            isConcurrent = true;
        }
        else if ((this.ConcurencyId == null && concurencyId != null) || (this.ConcurencyId != null && concurencyId == null))
        {
            isConcurrent = false;
        }
        else
        {
            isConcurrent = this.ConcurencyId.Equals(concurencyId);
        }
        return isConcurrent;
    }

    protected void SaveChanges()
    {
        PerformValidation();
        if (this.IsDeleted)
        {
            if (!IsNew)
            {
                Delete();   
            }
        }
        else
        {
            if (this.IsNew)
            {
                concurencyId = Insert();
                isNew = false;
            }
            else
            {
                concurencyId = Update();
            }
        }
    }

    protected abstract object Insert();
    protected abstract object Update();
    protected abstract void Delete();

    protected void ExecuteCommand(SqlCommand cmd)
    {
        //Add the return
        SqlHelper.AddReturnParameter(cmd, SqlDbType.Int, null, null, null);
        
        SqlConnection connection = new SqlConnection(ConnectionString);
        
        SqlHelper.ExecuteNonQueryCommand(cmd, connection);
        
        int ret = SqlHelper.GetReturnValue<int>(cmd);
        if (ret == -100)
        {
            if (cmd.Transaction != null)
            {
                cmd.Transaction.Rollback();
            }
            throw new Acms.Exceptions.ConcurencyException();
        }
        else if (ret == -200)
        {
            if (cmd.Transaction != null)
            {
                cmd.Transaction.Rollback();
            }
            throw new Acms.Exceptions.IdNotFoundException();
        }
    }

    protected static string ConnectionString
    {
        get { return ConfigurationManager.ConnectionStrings["CCC_ConnectionString"].ConnectionString; }
    }

}
